#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::unique_ptr;

class Strategy
{
public:
    virtual void execute() = 0;
    virtual ~Strategy() {}
};

class ConcreteStrategyA
: public Strategy
{
public:
    virtual void execute() override
    {
        cout << "ConcreteStrategyA的策略" << endl;
    }
};

class ConcreteStrategyB
: public Strategy
{
public:
    virtual void execute() override
    {
        cout << "ConcreteStrategyB的策略" << endl;
    }
};

class Context
{
public:
    //更改策略
    void setStrategy(Strategy *pstr)
    {
        _pstr = pstr;
    }

    void executeStrategy()
    {
        if(_pstr)
        {
            _pstr->execute();//多态
        }
    }
private:
    Strategy *_pstr;
};

void test()
{
    //创建两个策略者对象
    unique_ptr<Strategy> pa(new ConcreteStrategyA());
    unique_ptr<Strategy> pb(new ConcreteStrategyB());

    unique_ptr<Context> ptx(new Context());

    //如果此时使用的是策略A
    ptx->setStrategy(pa.get());
    ptx->executeStrategy();

    cout << endl << endl;
    //将策略从A改为B
    ptx->setStrategy(pb.get());
    ptx->executeStrategy();

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

